package textenmu;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class TextflectEnum {
    public static void refectPrivateField() {
        try {
            Class<?> classStudent = Class.forName("textenmu.TextEnum");
            Constructor<?> declearedConstructorStudent =
                    /*这里传参数传4个是因为TextflectEnum这个枚举类默认是继承于Enum这个父类的所以在传参数的时候要
                    先帮助父类的构造方法完成初始化，然后再初始化子类的构造方法*/
                    classStudent.getDeclaredConstructor(String.class,int.class,int.class,String.class);

             declearedConstructorStudent.setAccessible(true);

            Object objectStudent = declearedConstructorStudent.newInstance("父类参数",111,666,"绿色");
            TextEnum textEnum = (TextEnum) objectStudent;
            System.out.println("获得枚举的私有构造方法："+textEnum);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        refectPrivateField();//  结论：   #####    在类外是不能反射枚举对象的
    }
}
